package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.UserOpLog;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.UserInfo;
import com.tbit.uqbike.object.pojo.Forbidden;
import com.tbit.uqbike.object.pojo.ForbiddenAddDTO;
import com.tbit.uqbike.object.pojo.dto.ForbiddenUpdateDTO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.business.ForbiddenService;
import com.tbit.uqbike.webmanager.business.user_op_log.service.UserOpLogService;
import com.tbit.uqbike.webmanager.controller.BaseController;
import com.tbit.uqbike.webmanager.dao.core.ForbiddenDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 禁行区接口实现类
 *
 * @Author:BUGTian
 * @DATE: 2023/1/8
 */
@Service
public class ForbiddenServiceImpl extends ServiceImpl<ForbiddenDao, Forbidden> implements ForbiddenService {

    @Autowired
    private ForbiddenDao forbiddenDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private UserOpLogService userOpLogService;

    @Override
    public PageResult<Forbidden> queryPage(Integer page, Integer size, Integer accountId, String name, Integer mapType) {
        QueryWrapper<Forbidden> queryWrapper = new QueryWrapper<Forbidden>().eq("accountId", accountId);
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name", "%" + name + "%");
        }
        PageInfo<Forbidden> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> forbiddenDao.selectList(queryWrapper));
        List<Forbidden> list = pageInfo.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(a -> calibrateService.calibrateForbidden(a, MapConstant.MAP_ORI, mapType, false));
        }
        return new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public void updateForbidden(ForbiddenUpdateDTO dto) {
        if (count(new QueryWrapper<Forbidden>().eq("accountId", dto.getAccountId()).eq("name", dto.getName()).ne("forbiddenId", dto.getForbiddenId())) > 0) {
            throw new BaseException("该运营区内有重复的禁行区名称");
        }
        dto.setMapType(dto.getMapType() == null ? MapConstant.MAP_GOOGLE : dto.getMapType());
        Forbidden forbidden = getById(dto.getForbiddenId());
        if (forbidden == null) {
            throw new BaseException("禁行区不存在");
        }
        BeanUtils.copyProperties(dto, forbidden);
        calibrateService.calibrateForbidden(forbidden, dto.getMapType(), MapConstant.MAP_ORI, true);
        updateById(forbidden);

        // 插入用户操作日志
        UserInfo loginInfo = BaseController.fetchUserInfo();
        userOpLogService.insertSingle(UserOpLog.builder()
                .accountId(forbidden.getAccountId())
                .opUser(loginInfo.getName())
                .opUserPhone(loginInfo.getPhone())
                .operation("编辑禁行区")
                .opPlatform(OperationPlatform.MANAGER.code)
                .opTime(LocalDateTime.now())
                .result(true)
                .build());
    }

    @Override
    public void add(ForbiddenAddDTO dto) {
        if (count(new QueryWrapper<Forbidden>().eq("accountId", dto.getAccountId()).eq("name", dto.getName())) > 0) {
            throw new BaseException("该运营区内有重复的禁行区名称");
        }
        dto.setMapType(dto.getMapType() == null ? MapConstant.MAP_GOOGLE : dto.getMapType());
        Forbidden forbidden = new Forbidden();
        BeanUtils.copyProperties(dto, forbidden);
        calibrateService.calibrateForbidden(forbidden, dto.getMapType(), MapConstant.MAP_ORI, true);
        save(forbidden);
    }


}
